<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>下拉菜单</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" >
    <meta name="generator" content="www.leipi.org" />
    <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
    <!--[if lte IE 6]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-ie6.css">
    <![endif]-->
    <!--[if lte IE 7]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/ie.css">
    <![endif]-->
    <link rel="stylesheet" href="leipi.style.css">
    <link rel="stylesheet" href="../../../../adminlte3.0/plugins/select2/css/select2.css">
    <script type="text/javascript" charset="utf-8" src="../../../js/jquery-1.7.2.min.js?2023"></script>
    <script src="../../../../common/jsite.js"></script>
    <script src="../../../../adminlte3.0/plugins/select2/js/select2.full.js"></script>
    <script src="../../../../adminlte3.0/plugins/select2/js/i18n/zh-CN.js"></script>
    <script src="../../../../modules/icheck-select-init/icheck-select-init.js"></script>
    <script type="text/javascript" src="../dialogs/internal.js"></script>
    <script type="text/javascript">
function createElement(type, name)
{     
    var element = null;     
    try {        
        element = document.createElement('<'+type+' name="'+name+'">');     
    } catch (e) {}   
    if(element==null) {     
        element = document.createElement(type);     
        element.appenname = name;
    } 
    return element;     
}

// Add a new option to a SELECT object (combo or list)
function fnAddComboOption( combo, optionText, optionValue, documentObject, index ) {
    var oOption ;
    if ( documentObject ) {
        oOption = documentObject.createElement("option") ;
    } else {
        oOption = document.createElement("option") ;
    }
    if ( index != null ) {
        combo.options.add( oOption, index ) ;
    } else {
        combo.options.add( oOption ) ;
    }
    oOption.innerHTML = optionText.length > 0 ? fnHTMLEncode( optionText ) : '&nbsp;' ;
    oOption.value     = optionValue ;
    return oOption ;
}
function fnHTMLEncode( text ) {
    if ( !text ) {
        return '' ;
    }
    text = text.replace( /&/g, '&amp;' ) ;
    text = text.replace( /</g, '&lt;' ) ;
    text = text.replace( />/g, '&gt;' ) ;
    return text ;
}

function fnSetAttribute( element, attName, attValue ) {
    if ( attValue == null || attValue.length == 0 ){
        element.removeAttribute( attName, 0 ) ;
    } else {
        element.setAttribute( attName, attValue, 0 ) ;
    }
}


var oNode = null,oListText='', thePlugins = 'selectperson';

$(document).ready(function(){
    $("#dictlist").change(function() {
        var dictType = $(this).val();
        js.print("change===" + dictType);
        js.ajaxRequest("http://demo.jsite.org.cn/jsite/a/sys/user/listData", {'pageNo':1,'pageSize':1000,'id':dictType}, function (data) {
            var tableData = "";
            for(var i=0;i<data.list.length;i++){
                tableData += "<tr>";
                tableData += "<td>"+data.list[i].id+"</td>"
                tableData += "<td>"+data.list[i].name+"</td>"
                tableData += "<td>"+data.list[i].loginName+"</td>"
                tableData += "<td>"+data.list[i].mobile+"</td>"
                tableData+="</tr>"
            }

            $("#tbodyDict").html(tableData)
        }, true);
    });

    js.ajaxRequest("http://demo.jsite.org.cn/jsite/a/sys/user/listData", {'pageNo':1,'pageSize':1000}, function (data) {
        $("#dictlist").append("<option></option>");
        $.each(data.list, function(index,item){
            $("#dictlist").append("<option value='" + item.id + "'>" + item.name + "</option>");
        });

        if( UE.plugins[thePlugins].editdom ){
            oNode = UE.plugins[thePlugins].editdom;
            var gTitle = oNode.getAttribute('title').replace(/&quot;/g,"\"")
                ,gWidth=oNode.getAttribute('orgwidth')
                ,gSize=oNode.getAttribute('size')
                ,gType=oNode.getAttribute('orgType');
            gTitle = gTitle==null ? '' : gTitle;
            $G('orgname').value = gTitle;
            $G('orgsize').value = gSize;
            $G('orgwidth').value = gWidth;

            js.print("请求===" + gType);
            $("#dictlist").val(gType).trigger('change');
        }

    }, true);
});

    </script>
</head>
<body>
<div class="content">
    <table class="table table-bordered table-striped table-hover">
     <tr>
        <th><span>控件名称</span><span class="label label-important">*</span></th>
        <th><span>控件样式</span> </th>
    </tr>
    <tr>
        <td><input id="orgname" placeholder="必填项" type="text"/></td>
        <td> 宽：<input id="orgwidth" type="text" value="150" class="input-small span1"/> px&nbsp;&nbsp;&nbsp;&nbsp;高：<input id="orgsize" type="text" class="input-small span1" value="1"/> 行</td>
    </tr>
    <tr>
        <th><span class="pull-right">选择字典</span></th>
        <td> <select id="dictlist" ></select> </td>
    </tr>
    <tr>
        <th colspan="2">
            <span>列表值</span>
        </th>
    </tr>
    <tr>
        <td colspan="2">
            <div style="height: 150px;overflow-y: auto;">
                <table id="tbodyDict" >
                </table>
            </div>

        </td>
    </tr>
    </table>
</div>
<script type="text/javascript">

dialog.oncancel = function () {
    if( UE.plugins[thePlugins].editdom ) {
        delete UE.plugins[thePlugins].editdom;
    }
};
dialog.onok = function (){
    if( $G('orgname').value == '') {
        js.showWarningMessage('控件名称不能为空');
        return false;
    }
    // if( $G('dictlist').value == '' ) {
    //     js.showWarningMessage('请添加下拉菜单选项！');
    //     return false;
    // }
    var gSize = $G('orgsize').value ;
    if ( gSize == null || isNaN( gSize ) || gSize < 1 ) {
        gSize = '' ;
    }
    var gWidth=$G('orgwidth').value;
    var distlist = $G('dictlist').value;
    oListText = $G('dictlist');

    var tb=document.getElementById("tbodyDict");    //获取table对像
    var rows=tb.rows;
    var start = '';
    if (rows.length == 0 || distlist == '') {
        start = start;
    } else {
        var cells=rows[0].cells;
        start = cells[0].innerHTML;
    }
    console.log("初始选择" + start)

    if( !oNode ) {
        try {
            //oNode = document.createElement("select"); 
            oNode = createElement('select',$G('orgname').value);
            oNode.setAttribute('title',$G('orgname').value);
            oNode.setAttribute('leipiPlugins',thePlugins );
            oNode.setAttribute('size',gSize);
            oNode.setAttribute('orgType', $G('dictlist').value);

            if ( $G('orgwidth').value!= '' ) {
                oNode.style.width =  $G('orgwidth').value+ 'px';
            }
            if( gWidth != '' ) {
                oNode.style.width = gWidth + 'px';
                oNode.setAttribute('orgwidth',gWidth );
            }

            for ( var i = 0 ; i < oListText.options.length ; i++ ) {
                var sText = oListText.options[i].value;
                var sTextInner = oListText.options[i].innerHTML;
                if ( sText.length == 0 ) {
                    sText = sText ;
                }
                var oOption = fnAddComboOption( oNode, sTextInner, sText) ;
                // console.log(sText + "======" + start)
                if ( sText == start ) {
                    fnSetAttribute( oOption, 'selected', 'selected' ) ;
                    oOption.selected = true ;
                }

                // if ( sText == start ) {
                //     fnSetAttribute( oOption, 'selected', 'selected' ) ;
                //     oOption.selected = true ;
                // }

            }

            //firefox要利用span
            editor.execCommand('insertHtml','<span leipiplugins="selectperson">'+oNode.outerHTML+'&nbsp;&nbsp;</span>');
            return true ;
        } catch ( e ) {
            try {
                editor.execCommand('error');
            } catch ( e ) {
                alert('控件异常，请到 [雷劈网] 反馈或寻求帮助！');
            }
            return false;
        }
    } else {
        oNode.setAttribute('title', $G('orgname').value);
        oNode.setAttribute('name', $G('orgname').value);
        oNode.setAttribute('size',gSize);
        oNode.setAttribute('orgType', $G('dictlist').value);
        if( gWidth != '' ) {
			oNode.style.width = gWidth + 'px';
			oNode.setAttribute('orgwidth',gWidth );
		}

        // Remove all options.
        while ( oNode.options.length > 0 ){
            oNode.remove(0) ;
        }
        for ( var i = 0 ; i < oListText.options.length ; i++ ) {
            var sText = oListText.options[i].value;
            var sTextInner = oListText.options[i].innerHTML;

            // console.log(sText + "=====" + sTextInner)
            if ( sText.length == 0 ) {
                sText = sText ;
            }
            var oOption = fnAddComboOption( oNode, sTextInner, sText) ;
            // if ( sText == start ) {
            //     fnSetAttribute( oOption, 'selected', 'selected' ) ;
            //     oOption.selected = true ;
            // }
        }
        delete UE.plugins[thePlugins].editdom;
    }
};


</script>
</body>
</html>